package co.yixiang.mall.biz.util;

import co.yixiang.mall.biz.commission.mapper.CommissionService;
//import co.yixiang.mall.biz.commission.mapper.ServiceMapper;
import co.yixiang.mall.biz.commission.mapper.StoreService;
import co.yixiang.mall.biz.entity.SysRetailType;
import co.yixiang.mall.biz.entity.YxSystemStoreService;
import co.yixiang.mall.common.mall.domain.YxUser;
import co.yixiang.mall.common.mallsetting.domain.TbBrokerageFreeze;
import co.yixiang.mall.common.order.domain.YxStoreOrder;
import co.yixiang.mall.common.shop.domain.YxSystemStore;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.log4j.Log4j2;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
@Log4j2
public class OrderTypeUtil {
    /**
     * 返佣工具包
     * */
    public static BigDecimal Centre(YxSystemStore shop, BigDecimal service, CommissionService commissionService, SysRetailType shopType, StoreService storeService)throws RuntimeException {
        BigDecimal fuwu = new BigDecimal(0.0);
        /** 查询订单 获得订单的实付金额  */
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("store_id", shop.getId());
        wrapper.eq("freeze", 1);
        wrapper.eq("settlement",0);
        List<YxStoreOrder> orderList = commissionService.selectList(wrapper);
        if (orderList.size() > 0) {
            for (YxStoreOrder x : orderList) {
                BigDecimal xiangQing = new BigDecimal(0.0);
                /** 获得实付金额 */
                BigDecimal payPrice = x.getPayPrice();
                if (x.getOrderType() == 2) {
                    /** 获得社区店的服务费比例 */
                    BigDecimal service1 = shopType.getService();
                    /** 类型是2代表是中心店向社区店发货 */
                    /** 计算出这一订单的服务费 */
                    xiangQing =  payPrice.multiply(service.subtract(service1));
                } else if (x.getOrderType() == 3||x.getOrderType() == 1) {
                    /** 类型1代表是社区店向消费者发货 类型3代表是中心店直接向消费者发货 */
                    xiangQing = payPrice.multiply(service);
                }  else if(x.getOrderType() == 4 || x.getOrderType() == 5){
                    continue;
                }else{
                    log.error("订单类型异常");
                    continue;
                }
                fuwu.add(xiangQing);
                x.setSettlement(1);
                commissionService.updateById(x);
                /** 将服务费详情放入到详情表中 */
                YxSystemStoreService service1 = new YxSystemStoreService();
                service1.setOrderId(x.getId());
                service1.setShopId(shop.getId());
                service1.setServiceCharge(xiangQing);
                service1.setAddTime(new Date());
                storeService.insert(service1);
            }
        }
        return fuwu;
    }
}
